using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Pokemon
{
    class Beam : Bullet
    {
        protected static List<Texture2D> _texture2Ds = new List<Texture2D>();

        protected static List<Vector2> _sizeTextures = new List<Vector2>();

        protected static List<int> _numFrame = new List<int>();

        protected static int _speed;

        protected static string _pathXML;//Chua duong dan den file dac ta XML

        protected bool _hit = false;

        public Beam(Game game, Unit owner, Unit target) : base(game, owner, target)
        {
            _position = new Vector2(owner.Position.X + owner.Size.X, owner.Position.Y - 10);
        }

        public new static void Initialize()
        {
            XDocument doc = XDocument.Load(_pathXML);
            XName name = XName.Get(@"Speed");
            
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _speed = Int32.Parse((definitions.First()).Value);

                name = XName.Get(@"Style");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        _texture2Ds.Add(_game.Content.Load<Texture2D>(xAttribute.Value));
                        xAttribute = definition.Attribute(@"NumFrame");
                        if (xAttribute != null)
                        {
                            var value = Int32.Parse(xAttribute.Value);
                            _numFrame.Add(value);
                        }

                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = Int32.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = Int32.Parse(xAttribute.Value);
                                _sizeTextures.Add(new Vector2(width, height));
                            }
                        }
                    }
                }
            }
        }


        public static void AddPath(string path)
        {
            _pathXML = path;
        }

        public void NextFrame()
        {
            _frameIndex = (_frameIndex + 1) % _numFrame[0];
        }


        public override void Update(GameTime gameTime)
        {
            _timeSpan += gameTime.ElapsedGameTime;

            base.Update(gameTime);

            if (_hit && _timeSpan.Milliseconds < 400)
                return;

            _timeSpan = TimeSpan.Zero;
            _position.X += 1;


            // neu chua toi muc tieu thi chi ve ra trang thai bullet chua no
            NextFrame();
            if (_frameIndex >= 2 && !_hit)
                _frameIndex = 0;            
        }


        public override void Draw(GameTime gameTime)
        {
            try
            {
                _timeSpan += gameTime.ElapsedGameTime;

                if (_hit == false)
                {
                    _SpriteBatch.Draw(_texture2Ds[0], new Vector2(_position.X, _position.Y - 50), GetNextRect(), Color.White);

                }
                else
                {
                    _SpriteBatch.Draw(_texture2Ds[0],
                        new Vector2(_target.GetCenterPoint().X - _sizeTextures[0].X / (2 * _numFrame[0]),
                            _target.Owner.Position.Y - _sizeTextures[0].Y / _numFrame[0] - 25),
                        GetNextRect(), Color.White);
                    if (_frameIndex == _numFrame[0] - 1)
                    {
                        BulletManager b = (BulletManager)Game.Services.GetService(typeof(BulletManager));
                        b.Remove(this);
                    }
                    return;
                }

                _target = _owner.Enemy;
                if (_target != null)
                {
                    Rectangle rectangle = new Rectangle((int)_position.X, (int)_position.Y,
                            (int)_sizeTextures[0].X / _numFrame[0],
                            (int)_sizeTextures[0].Y);

                    // kiem tra xem thu vien dan bay da trung muc tieu chua
                    if (rectangle.Contains(_target.GetCenterPoint()))
                    {
                        // neu dan trung roi thi muc tieu bi mat mau
                        _target.TakeDamage(_owner.Damage, Color.Red);
                        _hit = true;

                        _frameIndex = 3;
                    }
                }      
            }
            catch (System.Exception ex)
            {
            	
            }                  
        }

        public Rectangle GetNextRect()
        {
            Rectangle rect;
            rect = new Rectangle(_frameIndex * (int)_sizeTextures[0].X / _numFrame[0], 0, (int)_sizeTextures[0].X / _numFrame[0], (int)_sizeTextures[0].Y);
            return rect;
        }

    }
}
